﻿using M_Ingredient.API.Application.Command.IngredientCommand;
using M_Ingredient.Repository.Interface;
using MediatR;

namespace M_Ingredient.API.Application.CommandHandler.IngredientCommandHandler
{
    public class IngredientDeleteCommandHandler : IRequestHandler<IngredientDeleteCommand, int>
    {
        protected readonly IIngredientRepository ingredientRepository;

        public IngredientDeleteCommandHandler(IIngredientRepository ingredientRepository)
        {
            this.ingredientRepository = ingredientRepository;
        }

        public async Task<int> Handle(IngredientDeleteCommand request, CancellationToken cancellationToken)
        {
            var id = request.IngredientId;
            foreach (var item in id)
            {
                var res = await ingredientRepository.GetModelAsync(item);
                res.IsDeleted = true;

                await ingredientRepository.UpdateAsync(res);
            }
            return 1;
        }
    }
}
